﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Text.RegularExpressions;

namespace SupplyChainManagement
{
    public partial class EditForm : Form
    {
        private InventoryForm mainForm;
        private string invId, invAmount, invName;
        
        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(InventoryForm form, DataGridViewCellCollection cells)
        {
            mainForm = form;
            
            InitializeComponent();
            txtID.Text = invId = cells[0].Value.ToString();
            txtAmount.Text = invAmount = cells[cells.Count - 1].Value.ToString();
            txtName.Text = cells[1].Value.ToString(); 
            //InitializeFields();
        }

        /// <summary>
        /// Populate fields with the item's original data from the database
        /// </summary>
        public void InitializeFields()
        {
            //OdbcConnection dbMySQL = new OdbcConnection(strConnect);
            //try
            //{
            //    dbMySQL.Open();

            //    OdbcCommand sqlCommand = dbMySQL.CreateCommand();
            //    //assign the SQL command
            //    sqlCommand.CommandText = "select * from inventory where inv_id='" + invId + "'";

            //    //reader executes the select command
            //    OdbcDataReader sqlReader = sqlCommand.ExecuteReader();
            //    while (sqlReader.Read())
            //    {
            //        txtID.Text = sqlReader.GetString(0);
            //        txtName.Text = sqlReader.GetString(1);
                    
            //    }
            //    sqlReader.Close();
            //    sqlCommand.Dispose();

            //    dbMySQL.Close();
            //}
            //catch (OdbcException ex)
            //{
            //    Console.WriteLine("Database error\n\n{0}", ex.ToString());
            //}
            //finally
            //{
            //    if (dbMySQL != null) dbMySQL.Close();
            //}
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool isEdited = false;
            //check if any of the fields are empty
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtAmount.Text))
                MessageBox.Show("Data is not complete");
            //check if price only contains numbers
            else if (Regex.IsMatch(txtAmount.Text, @"^[0-9,-\.]+$"))
            {
                isEdited = EditEntry();
                if (isEdited)
                {
                    MessageBox.Show("Success!");
                    //move back to main form
                    this.Close();
                    mainForm.LoadDatabase();
                    mainForm.Enabled = true;
                    mainForm.Activate();
                }
            }
            else MessageBox.Show("Data is not valid.");            
            
        }

        private bool EditEntry()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();

                sqlCommand.CommandText = "update inventory set I_Name='" + txtName.Text + "', I_Amount='" + txtAmount.Text
                    + "' where I_Id='" + invId + "'";

                sqlCommand.ExecuteNonQuery();

                sqlCommand.Dispose();

                dbMySQL.Close();
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
            return true;
        }

        private void EditForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainForm.Enabled = true;
            mainForm.Activate();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
